package com.xh3140.android.widget.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.xh3140.android.widget.dialog.params.BaseParams;
import com.xh3140.android.widget.dialog.params.DialogParams;
import com.xh3140.android.widget.dialog.view.FooterView;
import com.xh3140.android.widget.dialog.view.HeaderView;
import com.xh3140.android.widget.dialog.view.RootView;

/**
 * 自定义对话框
 */
@SuppressWarnings("unused")
public class CustomDialog extends BaseDialog<View> {
    /**
     * 自定义对话框属性
     */
    private final Params mParams;

    /**
     * 自定义对话框属性数据类
     */
    private static final class Params {
        public final DialogParams dialog;
        public final BaseParams<CustomDialog> base;
        public final View view;

        private Params(@NonNull DialogParams dialog, @NonNull BaseParams<CustomDialog> base, @NonNull View view) {
            this.dialog = dialog;
            this.base = base;
            this.view = view;
        }
    }

    public CustomDialog(@NonNull Params params) {
        super(params.dialog);
        mParams = params;
    }

    @NonNull
    @Override
    protected RootView<View> initRootView(@NonNull Context context) {
        final CustomDialog dialog = this;
        return new RootView<View>(context) {
            @Override
            protected HeaderView initHeaderView(@NonNull Context context) {
                return new HeaderView(context, mParams.base.header);
            }

            @Override
            protected View initBodyView(@NonNull Context context) {
                return mParams.view;
            }

            @Override
            protected FooterView initFooterView(@NonNull Context context) {
                return new FooterView(context, dialog, mParams.base.footer);
            }
        };
    }

    /**
     * 自定义对话框构建器
     * 设置自定义对话框属性
     */
    public static final class Builder extends BaseDialog.Builder<CustomDialog, Builder> {
        /**
         * 对话框自定义主体视图
         */
        private View mCustomView;

        public Builder(@NonNull Context context, @IntRange(from = 0) int buttonCount) {
            super(context, buttonCount);
            mDialogParams.showDividers = -1;
        }

        public Builder(@NonNull FragmentActivity activity, @IntRange(from = 0) int buttonCount) {
            super(activity, buttonCount);
            mDialogParams.showDividers = -1;
        }

        public Builder(@NonNull Fragment fragment, @IntRange(from = 0) int buttonCount) {
            super(fragment, buttonCount);
            mDialogParams.showDividers = -1;
        }

        /**
         * 构建对话框
         * 这个方法应该在所有配置结束后调用
         */
        @Override
        public CustomDialog build() {
            return new CustomDialog(new Params(mDialogParams, mBaseParams, mCustomView));
        }

        /**
         * 设置自定义主体视图
         *
         * @param view 自定义主体视图
         */
        public Builder setBodyView(@NonNull View view) {
            mCustomView = view;
            return this;
        }

        /**
         * 设置自定义主体视图布局资源
         *
         * @param res 自定义主体视图布局资源
         */
        public Builder setBodyViewResource(@LayoutRes int res) {
            mCustomView = LayoutInflater.from(mDialogParams.context).inflate(res, null, false);
            return this;
        }
    }
}
